﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace JustTest.Admin
{
    public partial class BrowseTests : System.Web.UI.Page
    {
        JustTestDb context = new JustTestDb();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public IQueryable<JustTest.Models.Test> GridViewTests_GetData()
        {
            int selectedStatusNumber = int.Parse(this.DropDownListTestStatus.SelectedValue);
            TestStatus selectedStatus = (TestStatus)selectedStatusNumber;
            var tt = context.Tests.Where(t => t.Status == selectedStatus).ToList();

            return context.Tests.Where(t => t.Status == selectedStatus);
        }

        protected void DropDownListTestStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridViewTests.DataBind();
            string buttonText = null;

            int selectedStatusNumber = int.Parse(this.DropDownListTestStatus.SelectedValue);
            TestStatus selectedStatus = (TestStatus)selectedStatusNumber;

            switch (selectedStatus)
            {
                case TestStatus.UnApproved:
                    buttonText = "Aprove";
                    break;
                case TestStatus.Active:
                    buttonText = "Suspend";
                    break;
                case TestStatus.Suspended:
                    buttonText = "Activate";
                    break;
            }

            foreach (GridViewRow row in this.GridViewTests.Rows)
            {
                Button togleButton = (Button)row.FindControlRecursive("ButtonToggleTestStatus");
                togleButton.Text = buttonText;

                if (selectedStatus != TestStatus.UnApproved)
                {
                    Button rejectButton = (Button)row.FindControlRecursive("ButtonRejectTest");
                    rejectButton.Visible = false;
                }
            }

        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewTests_UpdateItem(int id)
        {
            JustTest.Models.Test item = context.Tests.Find(id);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                context.SaveChanges();

            }
        }

        protected void ButtonToggleTestStatus_Click(object sender, EventArgs e)
        {
            int testId = int.Parse(((Button)sender).CommandArgument);
            var test = context.Tests.Find(testId);

            switch (test.Status)
            {
                case TestStatus.UnApproved:
                    test.Status = TestStatus.Active;
                    test.MaxResult = test.Questions.Sum(q => q.Answers.Max(a => a.Value));
                    break;
                case TestStatus.Active:
                    test.Status = TestStatus.Suspended;
                    break;
                case TestStatus.Suspended:
                    test.Status = TestStatus.Active;
                    break;
            }

            context.SaveChanges();
            DropDownListTestStatus_SelectedIndexChanged(sender, e);
        }

        protected void ButtonRejectTest_Click(object sender, EventArgs e)
        {
            int testId = int.Parse(((Button)sender).CommandArgument);
            var test = context.Tests.Find(testId);

            test.Status = TestStatus.Unpublished;
            context.SaveChanges();
            DropDownListTestStatus_SelectedIndexChanged(sender, e);
        }
    }
}